﻿using Caliburn.Micro;
using KouDai.WinPad.Helper;
using KouDai.WinPad.Models;
using KouDai.WinPad.Services;
using NextEngine.Extens;
using NextEngine.HttpEngine;
using System.Collections.Generic;
using System.Diagnostics;
using Windows.ApplicationModel;

namespace KouDai.WinPad.ViewModels
{
    public class SearchPageViewModel : KouDaiScreen
    {
        #region Fields
        private readonly INavigationService _navigationService;
        private BindableCollection<HoneyEntity> _searchItems = new BindableCollection<HoneyEntity>();

        private int _page = 1;
        private string _parameter;
        private string _resultCount;
        #endregion

        #region Constructor

        public SearchPageViewModel()
        {
            if (DesignMode.DesignModeEnabled)
            {
                LoadDesignData();
            }
        }

        public SearchPageViewModel(INavigationService navigationService)
            : base(navigationService)
        {
            _navigationService = navigationService;
        }

        #endregion

        #region Properties

        public BindableCollection<HoneyEntity> SearchItems
        {
            get { return _searchItems; }
            set { _searchItems = value; }
        }

        public string ResultCount
        {
            get { return _resultCount; }
            set
            {
                if (value != _resultCount)
                {
                    _resultCount = value;
                    this.NotifyOfPropertyChange(() => this.ResultCount);
                }
            }
        }

        public string Parameter
        {
            get { return _parameter; }
            set
            {
                if (value != _parameter)
                {
                    _parameter = value;
                    this.NotifyOfPropertyChange(() => this.Parameter);
                }
            }
        }

        public override bool Loading
        {
            get
            {
                return base.Loading;
            }
            set
            {
                if (value != base._loading)
                {
                    base._loading = value;
                    BusyInicator.Instance.IsBusy = value;
                    this.NotifyOfPropertyChange(() => this.Loading);
                }
            }
        }

        public string KeyWord { get; set; }
        public int Index { get; set; }

        #endregion

        #region Method

        #endregion


        public override void LoadDesignData()
        {
            this.DisplayName = "搜索'" + "" + KeyWord + "'";
            ResultCount = "共有" + "2400" + "个搜索结果";

            List<HoneyEntity> honeyList = new List<HoneyEntity>();
            honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img4.geilicdn.com/taobao22676688431_310x310.jpg", Price = "10000000", Name = "非常好的衣服，男装，必须要买的，呵呵" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.33, ImageUrlForIphone = "http://img3.geilicdn.com/taobao16620567956_310x310.jpg", Price = "100", Name = "非常好的衣服，男装，必须要买的，呵呵" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.00, ImageUrlForIphone = "http://img2.geilicdn.com/taobao18678095176_310x310.jpg", Price = "100", Name = "非常好的衣服，男装，必须要买的，呵呵" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img0.geilicdn.com/taobao17626934651_310x232.jpg", Price = "100", Name = "非常好的衣服，男装，必须要买的，呵呵" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.03, ImageUrlForIphone = "http://img1.geilicdn.com/taobao22903704025_310x310.jpg", Price = "100", Name = "非常好的衣服，男装，必须要买的，呵呵" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.57, ImageUrlForIphone = "http://img4.geilicdn.com/taobao14747655233_310x310.jpg", Price = "100", Name = "非常好的衣服，男装，必须要买的，呵呵" });
            honeyList.Add(new HoneyEntity { ImgRatio = 0.99, ImageUrlForIphone = "http://img4.geilicdn.com/taobao17481641771_310x310.jpg", Price = "100", Name = "非常好的衣服，男装，必须要买的，呵呵" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img1.geilicdn.com/taobao10519429184_310x310.jpg", Price = "100", Name = "非常好的衣服，男装，必须要买的，呵呵" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.15, ImageUrlForIphone = "http://img0.geilicdn.com/taobao16335513321_310x310.jpg", Price = "100", Name = "非常好的衣服，男装，必须要买的，呵呵" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.46, ImageUrlForIphone = "http://img1.geilicdn.com/taobao21812960575_310x310.jpg", Price = "100", Name = "非常好的衣服，男装，必须要买的，呵呵" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.12, ImageUrlForIphone = "http://img0.geilicdn.com/taobao13862322244_310x310.jpg", Price = "100", Name = "非常好的衣服，男装，必须要买的，呵呵" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.38, ImageUrlForIphone = "http://img1.geilicdn.com/taobao19227963217_310x310.jpg", Price = "100", Name = "非常好的衣服，男装，必须要买的，呵呵" });
            honeyList.Add(new HoneyEntity { ImgRatio = 0.99, ImageUrlForIphone = "http://img1.geilicdn.com/taobao18840571801_310x310.jpg", Price = "100", Name = "非常好的衣服，男装，必须要买的，呵呵" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.00, ImageUrlForIphone = "http://img1.geilicdn.com/taobao17422033364_310x310.jpg", Price = "100", Name = "非常好的衣服，男装，必须要买的，呵呵" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img0.geilicdn.com/taobao13324773512_310x310.jpg", Price = "100", Name = "非常好的衣服，男装，必须要买的，呵呵" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.35, ImageUrlForIphone = "http://img1.geilicdn.com/taobao19557264865_310x310.jpg", Price = "100", Name = "非常好的衣服，男装，必须要买的，呵呵" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.53, ImageUrlForIphone = "http://img3.geilicdn.com/taobao13192759861_310x310.jpg", Price = "100", Name = "非常好的衣服，男装，必须要买的，呵呵" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.33, ImageUrlForIphone = "http://img2.geilicdn.com/taobao9685026821_310x310.jpg", Price = "100", Name = "非常好的衣服，男装，必须要买的，呵呵" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img4.geilicdn.com/taobao18453287970_310x310.jpg", Price = "100", Name = "非常好的衣服，男装，必须要买的，呵呵" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.60, ImageUrlForIphone = "http://img3.geilicdn.com/taobao19061667201_310x310.jpg", Price = "100", Name = "非常好的衣服，男装，必须要买的，呵呵" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.41, ImageUrlForIphone = "http://img2.geilicdn.com/taobao19011767192_310x232.jpg", Price = "100", Name = "非常好的衣服，男装，必须要买的，呵呵" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.50, ImageUrlForIphone = "http://img1.geilicdn.com/taobao19106083432_310x310.jpg", Price = "100", Name = "非常好的衣服，男装，必须要买的，呵呵" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.53, ImageUrlForIphone = "http://img3.geilicdn.com/taobao20045912578_310x310.jpg", Price = "100", Name = "非常好的衣服，男装，必须要买的，呵呵" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.33, ImageUrlForIphone = "http://img0.geilicdn.com/taobao14317975864_310x310.jpg", Price = "100", Name = "非常好的衣服，男装，必须要买的，呵呵" });
            honeyList.Add(new HoneyEntity { ImgRatio = 0.67, ImageUrlForIphone = "http://img1.geilicdn.com/taobao21768632372_310x310.jpg", Price = "100", Name = "非常好的衣服，男装，必须要买的，呵呵" });
            honeyList.Add(new HoneyEntity { ImgRatio = 1.51, ImageUrlForIphone = "http://img4.geilicdn.com/taobao22945192213_310x310.jpg", Price = "100", Name = "非常好的衣服，男装，必须要买的，呵呵" });

            honeyList.ToObservableCollection(this.SearchItems);
        }


        protected override void OnInitialize()
        {
            base.OnInitialize();
            InitData();
        }

        protected override void OnViewAttached(object view, object context)
        {
            base.OnViewAttached(view, context);
        }

        protected override void OnActivate()
        {
            base.OnActivate();
        }

        protected override void OnViewReady(object view)
        {
            base.OnViewReady(view);
        }

        protected override void OnViewLoaded(object view)
        {
            base.OnViewLoaded(view);
        }

        protected override void OnDeactivate(bool close)
        {
            base.OnDeactivate(close);
        }

        private void InitData()
        {
            if (Parameter != null)
            {
                KeyWord = Parameter;
                this.DisplayName = "搜索'" + "" + KeyWord + "'"; 
            }
            else
            {
                if (Index == 0)
                {
                    KeyWord = "女装";
                }
                else if (Index == 1)
                {
                    KeyWord = "女包";
                }
                else if (Index == 2)
                {
                    KeyWord = "男鞋";
                }
                else if (Index == 3)
                {
                    KeyWord = "配饰";
                }
                else if (Index == 4)
                {
                    KeyWord = "童装";
                }
                else if (Index == 5)
                {
                    KeyWord = "女鞋";
                }
                else if (Index == 6)
                {
                    KeyWord = "男装";
                }
                else if (Index == 7)
                {
                    KeyWord = "男包";
                }
                else if (Index == 8)
                {
                    KeyWord = "家居";
                }
                else if (Index == 9)
                {
                    KeyWord = "数码";
                }
                this.DisplayName = KeyWord;
            }
         

            GetSearchHoneyItems();
        }

        private async void GetSearchHoneyItems()
        {
            if (!this.Loading)
            {
                this.Loading = true;

                BackData searchBackData = await ServiceManager.GetSearchResult(KeyWord, _page);
                if (searchBackData.IsSuccess)
                {
                    SearchResultEntity searchResultEntity = searchBackData.ResposeData as SearchResultEntity;
                    if (searchResultEntity.SearchResults.Count <= 0)
                    {
                        return;
                    }
                    if (Parameter != null)
                    {
                        ResultCount = "共有" + searchResultEntity.Count + "个搜索结果";
                    }
                    searchResultEntity.SearchResults.ToObservableCollection(this.SearchItems);
                    this._page++;
                }
                else
                {
                    Debug.WriteLine("Search Request Data Error.." + searchBackData.Msg.ToString());
                }
                this.Loading = false;
            }
        }
    }
}
